<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 扁平转树形
        var data = [
            {
                code: "ZCB",
                companyId: "1",
                createTime: null,
                id: "1175310929766055936",
                introduce: "总裁办公室",
                manager: "孙财",
                managerId: null,
                name: "总裁",
                pid: ""
            },
            {
                code: "XZB",
                companyId: "1",
                createTime: null,
                id: "1175311213774962688",
                introduce: "1111",
                manager: "罗晓晓",
                managerId: null,
                name: "行政部",
                pid: ""
            },
            {
                code: "RSB",
                companyId: "1",
                createTime: null,
                id: "1175311267684352000",
                introduce: null,
                manager: "文吉星",
                managerId: "1071632760222810112",
                name: "人事部",
                pid: ""
            },
            {
                code: "",
                companyId: "1",
                createTime: null,
                id: "1175311325720936448",
                introduce: null,
                manager: null,
                managerId: null,
                name: "财务部",
                pid: ""
            },
            {
                code: "JSB",
                companyId: "1",
                createTime: null,
                id: "1175311373083017216",
                introduce: "sss",
                manager: "罗晓晓",
                managerId: null,
                name: "技术部",
                pid: ""
            },
            {
                code: "YYB",
                companyId: "1",
                createTime: null,
                id: "1175311418004013056",
                introduce: null,
                manager: null,
                managerId: null,
                name: "运营部",
                pid: ""
            },
            {
                code: "SCB",
                companyId: "1",
                createTime: null,
                id: "1175311466846683136",
                introduce: null,
                manager: "武高丽",
                managerId: "1063705989926227968",
                name: "市场部",
                pid: ""

            },
            {
                code: null,
                companyId: "1",
                createTime: null,
                id: "1235395178363559936",
                introduce: null,
                manager: null,
                managerId: null,
                name: "财务核算部",
                pid: "1175311325720936448"
            },
            {
                code: null,
                companyId: "1",
                createTime: null,
                id: "1235398264104624128",
                introduce: null,
                manager: null,
                managerId: null,
                name: "税务管理部",
                pid: "1175311325720936448"
            },
            {
                code: null,
                companyId: "1",
                createTime: null,
                id: "1235398536969265152",
                introduce: null,
                manager: null,
                managerId: null,
                name: "薪资管理部",
                pid: "1175311325720936448"
            },
            {
                code: null,
                companyId: "1",
                createTime: null,
                id: "1235398608847052800",
                introduce: null,
                manager: null,
                managerId: null,
                name: "Java研发部",
                pid: "1175311373083017216"
            },
            {
                code: null,
                companyId: "1",
                createTime: null,
                id: "1235398661355544576",
                introduce: null,
                manager: null,
                managerId: null,
                name: "Python研发部",
                pid: "1175311373083017216"
            }]
        // function add(list, id) {
        //     var sp = []
        //     list.forEach(item => {
        //         if (item.pid === id) {
        //             const children = add(list, item.id)
        //             item.children = children
        //             sp.push(item)
        //         }
        //     })
        //     return sp
        // }
        // const ee = add(data, '')
        // console.log(ee);

        // function add(list, id) {
        //     var st = []
        //     list.forEach(item => {
        //         if (item.pid === id) {
        //             const children = add(list, item.id)
        //             item.children = children
        //             st.push(item)
        //         }
        //     })
        //     return st
        // }
        // const cc=add(data,'')
        // console.log(cc);
        // function add(list,id){
        //     var yy=[]
        //     list.forEach(item=>{
        //         if(item.pid===id){
        //             const children=add(list,item.id)
        //             item.children=children
        //             yy.push(item)
        //         }
        //     })
        //     return yy
        // }
        // console.log(add(data,''));
        // function add(list,id){
        //     var hh=[]
        //     list.forEach(item=>{
        //         if(item.pid===id){
        //             const children=add(list,item.id)
        //             item.children=children
        //             hh.push(item)
        //         }
        //     })
        //     return hh
        // }
        // console.log(add(data,''));
        // function add(list, id) {
        //     var ss = []
        //     list.forEach(item => {
        //         if (item.pid === id) {
        //             const children = add(list,item.id)
        //             item.children = children
        //             ss.push(item)
        //         }
        //     })
        //     return ss
        // }
        //     // console.log(add(data,''));
        //    function add(list,id){
        //        var uu=[]
        //        list.forEach(item=>{
        //            if(item.pid===id){
        //                const children=add(list,add)
        //                item.children=children
        //                uu.push(item)
        //            }
        //        })
        //        return uu
        //    }
        //    console.log(add(data,''));

        function add(list, id) {
            var ff = []
            list.forEach(item => {
                if (item.pid === id) {
                    const children = add(list, item.id)
                    item.children = children
                    ff.push(item)
                }
            })
            return ff
        }
        console.log(add(data, ''));
        //    树形转扁平
        // function str(list) {
        //     var dd = []
        //     list.forEach(obj => {
        //         if (obj.children) {
        //             const b = str(obj.children)
        //             dd.push(...b)
        //         }
        //         dd.push(obj)
        //     })
        //     return dd
        // }
    //     function see(list) {
    //         var aa = []
    //         list.forEach(obj => {
    //             if (obj.children) {
    //                 const cc = see(obj.children)
    //                 aa.push(...cc)
    //             }
    //             aa.push(obj)
    //         })
    //         return aa
    //     }
    //    const stt=see(data)
    //    console.log(stt);
    function see(list){
        var rr=[]
        list.forEach(obj=>{
            if(obj.children){
                const cc=see(obj.children)
                rr.push(...cc)
            }
            rr.push(obj)

        })
        return rr
    }
    const dd=see(data)
    console.log(dd);
    </script>
</body>

</html>